package com.wandian.ui.controls
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;


	public class List extends UIComponent
	{
		private var _itemHeight:int=30
		private var itemCount:int=1
		private var head:ListItem
		private var tail:ListItem
		private var itemContainer:Sprite
		private var listMask:Shape

		public function List(w:int=100, h:int=100)
		{
			super(w, h);
		}

		override protected function createChildren():void
		{
			listMask=new Shape()
			itemContainer=new Sprite();
			//itemContainer.mask=listMask

			this.addChild(itemContainer)
			this.addChild(listMask)
			var newItem:ListItem=new ListItem()
			head=tail=newItem
			newItem._index=0;
			newItem.draw(width, _itemHeight)
			itemContainer.addChild(newItem);
		}

		public function setPosition(pos:Number):void
		{
			if (pos < 0)
			{
				return;
			}
			itemContainer.y=pos * -1
			adjustListItemQueue(Math.floor(pos / _itemHeight))
		}


		override protected function layoutContents(w:int, h:int):void
		{
			var g:Graphics=listMask.graphics
			g.clear()
			g.beginFill(0xFF0000, 0.1)
			g.drawRect(0, 0, w, h);

			var count:int=Math.ceil(h / _itemHeight) + 1
			if (count == itemCount)
			{
				return;
			}
			var space:int=count - itemCount
			var order:Boolean=space > 0
			var item:ListItem=tail;

			if (order)
			{
				var index:int=tail._index
				for (var i:int=0; i < space; i++)
				{
					index++
					var newItem:ListItem=new ListItem()
					newItem.pre=item
					item.next=newItem
					newItem._index=index
					newItem.y=_itemHeight * index
					newItem.draw(width, _itemHeight)
					itemContainer.addChild(newItem)
					item=newItem
				}
			}
			else
			{
				for (var j:int=0; j < space; j++)
				{
					item=item.pre
				}
				item.next.pre=null
				item.next=null
			}
			tail=item
			itemCount=count
		}

		public function set itemHeight(v:int):void
		{
			if (_itemHeight == v)
			{
				return;
			}
			_itemHeight=v
		}

		public function get itemHeight():int
		{
			return _itemHeight
		}

		private function adjustListItemQueue(newHeadIndex:int):void
		{
			var oldHeadIndex:int=head._index
			if (newHeadIndex == oldHeadIndex)
			{
				return;
			}
			var space:int=newHeadIndex - oldHeadIndex
			var order:Boolean=space > 0
			space=Math.abs(space)
			var i:int=0
			var item:ListItem=null;
			var index:int=newHeadIndex
			if (space >= itemCount)
			{
				item=head
				for (i=0; i < itemCount; i++)
				{
					item._index=index
					item.y=_itemHeight * index
					item.draw(width, _itemHeight)
					index++
					item=item.next
				}
				return;
			}
			if (order)
			{
				//前面的需要往后补
				item=head;
				index=tail._index
				for (i=0; i < space; i++)
				{
					index++
					item._index=index
					item.y=_itemHeight * index
					item.draw(width, _itemHeight)
					item=item.next
				}
				tail.next=head;
				head.pre=tail
				head=item
				tail=item.pre
				head.pre=tail.next=null
			}
			else
			{
				//后面的需要往前补
				item=tail;
				index=head._index
				for (i=0; i < space; i++)
				{
					index--
					item._index=index
					item.y=_itemHeight * index
					item.draw(width, _itemHeight)
					item=item.pre
				}
				tail.next=head;
				head.pre=tail
				tail=item
				head=item.next
				head.pre=tail.next=null
			}
		}

		override protected function updateDisplay(w:int, h:int):void
		{

		}

	}

}
import flash.display.Graphics;
import flash.display.Sprite;
import flash.text.TextField;

class ListItem extends Sprite
{
	internal var pre:ListItem
	internal var next:ListItem
	internal var _data:Object
	internal var _index:int=0
	internal var tf:TextField

	public function ListItem()
	{
		super();
		tf=new TextField()
		this.addChild(tf)
	}

	public function get index():int
	{
		return _index
	}

	public function get data():Object
	{
		return _data;
	}

	public function draw(w:int, h:int):void
	{
		var g:Graphics=this.graphics
		g.clear();
		g.beginFill(0xFFFFFF - _index * 0x101010)
		g.drawRect(0, 0, w, h);
		tf.textColor=_index * 0xFF1010
		tf.text=_index + "";

	}
}
